/*
 *      Engine.h
 *      
 *      Copyright 2009 Daniel Penney <bikedude880@paqsario.tastywaffle.org>
 * 		Copyright 2005 Anders "Sion" Nissen
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef ENGINE_H
#define ENGINE_H

#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"

/** The base engine class. **/
class CEngine
{
private:
	/** Last iteration's tick value **/
	long m_lLastTick;

	/** Window width **/
	int m_iWidth;
	/** Window height **/
	int m_iHeight;

	/** Has quit been called? **/
	bool m_bQuit;

	/** Screen surface **/
	SDL_Surface* m_pScreen;

	/** Variables to calculate the frame rate **/
	/** Tick counter. **/
	int m_iFPSTickCounter;

	/** Frame rate counter. **/
	int m_iFPSCounter;

	/** Stores the last calculated frame rate. **/
	int m_iCurrentFPS;
	
	/** FPS Surface **/
	SDL_Surface* m_pFPSDisp;
	
	/** FPS Font **/
	TTF_Font* m_pFPSFont;
	
	/** FPS Surface **/
	char* m_sFPStext;
	
	/** FPS Display Toggle **/
	bool m_bFPStoggle;

protected:
	void DoThink();
	void DoRender();

	void SetSize( const int& iWidth, const int& iHeight );

	void HandleInput();
	void DisplayFPS();

public:
	CEngine();
	virtual ~CEngine();

	void Init();
	void Start();

	/** OVERLOADED - Data that should be initialized when the application starts. **/
	virtual void AdditionalInit	() {}

	/** OVERLOADED - All the games calculation and updating.
		@param iElapsedTime The time in milliseconds elapsed since the function was called last.
	**/
	virtual void Think( const int& iElapsedTime ) {}

	/** OVERLOADED - All the games rendering.
		@param pDestSurface The main screen surface.
	**/
	virtual void Render( SDL_Surface* pDestSurface ) {}

	/** OVERLOADED - Allocated data that should be cleaned up. **/
	virtual void End() {}

	// Input Core
	/** OVERLOADED - Keyboard key has been released.
		@param iKeyEnum The key number.
	**/
	virtual void KeyUp( const int& iKeyEnum ) {}

	/** OVERLOADED - Keyboard key has been pressed.
		@param iKeyEnum The key number.
	**/
	virtual void KeyDown( const int& iKeyEnum ) {}

	// Graphics Core
	SDL_Surface* GetSurface	();
	SDL_Surface* LoadImage( string );
	SDL_Rect GetTileClip( SDL_Surface*, int, int );
	void ApplySurface( int, int, SDL_Surface*, SDL_Surface* , SDL_Rect* );

	// Screen Core
	int GetFPS();
	int getScreenW() { return m_iWidth; }
	int getScreenH() { return m_iHeight; }
};

#endif // ENGINE_H
